package com.spirng.leetcode.day02;

public class RemoveNode {
    public static void main(String[] args) {
        RemoveNode removeNode = new RemoveNode();
        ListNode node = new ListNode(1);
        node.next=new ListNode(2);
        removeNode.removeNthFromEnd(node,2);
    }
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode fast,slow;
        ListNode dummyHead = new ListNode(0);
        dummyHead.next=head;
        fast=dummyHead;
        slow=dummyHead;
        for (int i = 0; i < n; i++) {
            fast=fast.next;
        }
        while (fast.next!=null){
            fast=fast.next;
            slow=slow.next;
        }
        slow.next=slow.next.next;
        //不能返回头节点,头节点可能会改变
        return dummyHead.next;
    }
}
